/**
* 
* Copyright [2007] [Sunny Liu]
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/
package ${appConfig.outputPackageName}.dao.ibatis;


import ${appConfig.outputPackageName}.model.${entity.javaName};
import ${appConfig.outputPackageName}.dao.${entity.javaName}DAO;
<#if entity.hasRelationships >
<#list entity.relationships as relationship>
import ${appConfig.outputPackageName}.model.${relationship.targetEntity.javaName};
</#list>
</#if>

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;



public class ${entity.javaName}DAOImpl extends SqlMapClientDaoSupport 
implements ${entity.javaName}DAO
{
	private static Logger log = LoggerFactory.getLogger(${entity.javaName}DAOImpl.class);
	
	
	
	public ${entity.javaName}DAOImpl()
	{
		super();
	}
	
	
	/**
	* this is a default find method for DAO, it will list all table contents
	* or limited by query.
	*/
	public List findAll${entity.javaName}()
	{
		if(log.isDebugEnabled()) log.debug("Entering findAll${entity.javaName}()");
		List ret = getSqlMapClientTemplate().queryForList("findAll${entity.javaName}", null);		
		
		if(log.isDebugEnabled()) log.debug("Exiting findAll${entity.javaName}()");	
		return ret;
	}
	
	
	<#if entity.hasPrimaryKey >	
	/**
	* Find domain model object from underline table base upon
	* primary key.
	*/	
	public ${entity.javaName} find${entity.javaName}ByKey(<#list entity.primaryKeyColumns as attribute>${attribute.javaType} ${attribute.javaName} <#if attribute_has_next>, </#if></#list>)
	{
		
		if(log.isDebugEnabled()) log.debug("Entering find${entity.javaName}ByKey()");
		${entity.javaName} ${entity.normalizedJavaName} = new ${entity.javaName}();
		<#list entity.primaryKeyColumns as attribute>
		${entity.normalizedJavaName}.set${attribute.setterGetterName}(${attribute.javaName});
		</#list>		
		${entity.javaName} ret = (${entity.javaName})getSqlMapClientTemplate().queryForObject("find${entity.javaName}ByKey", ${entity.normalizedJavaName});		
		
		if(log.isDebugEnabled()) log.debug("Exiting find${entity.javaName}ByKey() Primarykey=" + ${entity.normalizedJavaName});	
		return ret;
	}
	
	public ${entity.javaName} find${entity.javaName}ByKey(${entity.javaName} ${entity.normalizedJavaName})
	{
		if(log.isDebugEnabled()) log.debug("Entering find${entity.javaName}ByKey() Primarykey=" + ${entity.normalizedJavaName});
		${entity.javaName} ret = (${entity.javaName})getSqlMapClientTemplate().queryForObject("find${entity.javaName}ByKey", ${entity.normalizedJavaName});		
				
		if(log.isDebugEnabled()) log.debug("Exiting find${entity.javaName}ByKey() Primarykey=" + ${entity.normalizedJavaName});	
		return ret;
	}	
	
	</#if>
	
	
	
	/**
	* Insert a domain model into underline table.
	*/
	public ${entity.javaName} add${entity.javaName}(${entity.javaName} ${entity.normalizedJavaName})
	{
		if(log.isDebugEnabled()) log.debug("Entering add${entity.javaName}(${entity.javaName} model) ${entity.javaName}=" + ${entity.normalizedJavaName});
		Object key = getSqlMapClientTemplate().insert("add${entity.javaName}", ${entity.normalizedJavaName});	
		if(key!=null){
			<#if entity.primaryKeyIntegerType >
				<#switch entity.singlePrimaryKeyJavaType>
				  <#case "int">
			int intKey = ((Integer)key).intValue();
				<#list entity.primaryKeyColumns as attribute>
			${entity.normalizedJavaName}.set${attribute.setterGetterName}(intKey);
				</#list>			
				    <#break>
				  <#case "long">
			long longKey = ((Long)key).longValue();
				<#list entity.primaryKeyColumns as attribute>
			${entity.normalizedJavaName}.set${attribute.setterGetterName}(longKey);
				</#list>  
				    <#break>	
				 <#default>
				 if( key instanceof ${entity.javaName} ){
				 	${entity.normalizedJavaName} = (${entity.javaName})key;
				 }			 		  
				</#switch>		
			</#if>
		}
		if(log.isDebugEnabled()) log.debug("Exiting add${entity.javaName}(${entity.javaName} ${entity.normalizedJavaName}) ${entity.javaName}=" + ${entity.normalizedJavaName});	
		return ${entity.normalizedJavaName};	
	}
	
	
	/**
	* This method update underline row in relational database
	*/	
	public ${entity.javaName} update${entity.javaName}(${entity.javaName} ${entity.normalizedJavaName})
	{
		if(log.isDebugEnabled()) log.debug("Entering update${entity.javaName}(${entity.javaName} ${entity.normalizedJavaName}) ${entity.javaName}=" +${entity.normalizedJavaName});		
		getSqlMapClientTemplate().update("update${entity.javaName}", ${entity.normalizedJavaName});			
		if(log.isDebugEnabled()) log.debug("Exiting update${entity.javaName}(${entity.javaName} ${entity.normalizedJavaName}) ${entity.javaName}=" + ${entity.normalizedJavaName});	
		return ${entity.normalizedJavaName};
	}
	
	
	/**
	* This method delete underline row from relational database
	*/
	public ${entity.javaName} delete${entity.javaName}(${entity.javaName} ${entity.normalizedJavaName})
	{
		if(log.isDebugEnabled()) log.debug("Entering delete${entity.javaName}(${entity.javaName} ${entity.normalizedJavaName}) ${entity.javaName}=" + ${entity.normalizedJavaName});
		<#if entity.hasPrimaryKey >	
		${entity.javaName} ret =  delete${entity.javaName}ByKey(${entity.normalizedJavaName});
		<#else>
			${entity.javaName} ret = ${entity.normalizedJavaName};
			getSqlMapClientTemplate().delete("delete${entity.javaName}", ${entity.normalizedJavaName});
		</#if>
		if(log.isDebugEnabled()) log.debug("Exiting delete${entity.javaName}(${entity.javaName} ${entity.normalizedJavaName}) ${entity.javaName}=" + ${entity.normalizedJavaName});	
		return ret;	
	}
	<#if entity.hasPrimaryKey >		
	/**
	* This method delete underline row from relational database
	* with model's primary key.
	*/	
	public ${entity.javaName} delete${entity.javaName}ByKey(<#list entity.primaryKeyColumns as attribute>${attribute.javaType} ${attribute.javaName} <#if attribute_has_next>, </#if></#list>)
	{
		if(log.isDebugEnabled()) log.debug("Entering delete${entity.javaName}ByKey()");
		${entity.javaName} ${entity.normalizedJavaName} = new ${entity.javaName}();
		<#list entity.primaryKeyColumns as attribute>
		${entity.normalizedJavaName}.set${attribute.setterGetterName}(${attribute.javaName});
		</#list>		
		${entity.javaName} ret = find${entity.javaName}ByKey(${entity.normalizedJavaName});		
		if(ret!=null) 
			getSqlMapClientTemplate().delete("delete${entity.javaName}ByKey", ret);		
		if(log.isDebugEnabled()) log.debug("Exiting delete${entity.javaName}ByKey() Primarykey=" + ${entity.normalizedJavaName});	
		return ret;
	}
	protected ${entity.javaName} delete${entity.javaName}ByKey(${entity.javaName} ${entity.normalizedJavaName})
	{
		if(log.isDebugEnabled()) log.debug("Entering delete${entity.javaName}ByKey() Primarykey=" + ${entity.normalizedJavaName});
		${entity.javaName} ret = find${entity.javaName}ByKey(${entity.normalizedJavaName});		
		if(ret!=null) getSqlMapClientTemplate().delete("delete${entity.javaName}ByKey", ret);		
		if(log.isDebugEnabled()) log.debug("Exiting delete${entity.javaName}ByKey() Primarykey=" + ${entity.normalizedJavaName});	
		return ret;
	}	
	</#if>
	
	
	
	<#if entity.hasRelationships >
    <#list entity.relationships as relationship>    
    
    
    public <#if relationship.hasMultipleTarget >java.util.List<#else>${relationship.targetEntity.javaName}</#if>  find${relationship.targetEntity.javaName}By${entity.javaName}(${entity.javaName} ${entity.normalizedJavaName})
    {
    	if(log.isDebugEnabled()) log.debug("Entering find${relationship.targetEntity.javaName}By${entity.javaName}()");
		<#if relationship.hasMultipleTarget >   
		java.util.List ret = getSqlMapClientTemplate().queryForList("find${relationship.targetEntity.javaName}By${entity.javaName}",${entity.normalizedJavaName});		
	    <#else>
        ${relationship.targetEntity.javaName} ret = (${relationship.targetEntity.javaName}) getSqlMapClientTemplate().queryForObject("find${relationship.targetEntity.javaName}By${entity.javaName}",${entity.normalizedJavaName});
        </#if>		
		if(log.isDebugEnabled()) log.debug("Exiting find${relationship.targetEntity.javaName}By${entity.javaName}()");	
		return ret;
    } 
        
    </#list>
    </#if>
	
	
	
}
